package com.sduept.nwld.dataserver.controller.config;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.ParamManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.core.entity.SystemParameter;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@ManagedBean
@ViewScoped
public class PamaraterConfigController extends AbstractController {

    /**
     *
     */
    private static final long serialVersionUID = -5383504766732499178L;

    @Autowired
    private ParamManager manager;
    @Autowired
    private SuperDAO dao;

    private TreeNode root;
    private TreeNode currentSelect;
    private SystemParameter current = new SystemParameter();
    private SystemParameter currentRoot = new SystemParameter();
    private List<SystemParameter> paramConfigs = new ArrayList<SystemParameter>();
    private SystemParameter selected = new SystemParameter();

    @PostConstruct
    public void init() {
        root = new DefaultTreeNode();
        List<SystemParameter> configs = manager.listAllRootParams();
        if (configs != null && configs.size() != 0) {
            for (SystemParameter data : configs) {
                new LibTreeNode("root", data, root);
            }

        }
    }

    public void create() {
        current = new SystemParameter();
    }

    public void edit() {
        current = selected;
    }

    public void createRoot() {
        currentRoot = new SystemParameter();
    }

    public void editRoot() {
        currentRoot = (SystemParameter) currentSelect.getData();
    }

    public void saveRoot() {
        boolean isCreate = false;
        if (StringUtils.isEmpty(currentRoot.getId())) {
            isCreate = true;
        }
        if (isCreate) {
            currentRoot = manager.saveParam(currentRoot);
            new LibTreeNode("root", currentRoot, root);
            addSuccessMessage("添加成功！");

        } else {
            currentRoot = manager.saveParam(currentRoot);
            addSuccessMessage("编辑成功！");
            List<SystemParameter> list = manager.listParamsByPid(currentRoot.getId());
            ConcurrentHashMap<String, SystemParameter> paramMap = new ConcurrentHashMap<>();
            for (SystemParameter para : list) {
                if (!StringUtils.isEmpty(para.getName())) {
                    paramMap.put(para.getName(), para);
                }
            }
//			cache.getMap().put(currentRoot.getId(),paramMap); todo springboot
            currentSelect = null;
        }
    }

    public void save() {
        boolean isCreate = false;
        if (StringUtils.isEmpty(current.getId())) {
            isCreate = true;
        }
        if (isCreate) {
            SystemParameter selected = (SystemParameter) currentSelect.getData();
            current.setPid(selected.getId());
            current = manager.saveParam(current);
            paramConfigs.add(current);
            addSuccessMessage("添加成功！");

        } else {
            current = manager.saveParam(current);
            addSuccessMessage("编辑成功！");
            if (!StringUtils.isEmpty(current.getName())) {
//                cache.getMap().get(currentRoot.getId()).put(current.getId(), current);
//                cache.getKvmap().put(current.getId(), current);
            }
            selected = null;
        }
    }

    public void deleteById() {
        manager.deleteById(selected.getId());
        paramConfigs.remove(selected);
        addSuccessMessage("删除成功！");
        selected = null;
    }

    public void deleteRootById() {
        TreeNode node = currentSelect.getParent();
        node.getChildren().remove(currentSelect);
        dao.deleteAllByPid(currentRoot.getId(),SystemParameter.class);
        manager.deleteById(currentRoot.getId());
        addSuccessMessage("删除成功！");
        currentSelect = null;
        paramConfigs.clear();

    }

    public void onNodeSelect(NodeSelectEvent event) {
        currentSelect = event.getTreeNode();
        currentRoot = (SystemParameter) currentSelect.getData();
        paramConfigs = manager.listParamsByPid(currentRoot.getId());
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }

    public TreeNode getCurrentSelect() {
        return currentSelect;
    }

    public void setCurrentSelect(TreeNode currentSelect) {
        this.currentSelect = currentSelect;
    }

    public SystemParameter getCurrent() {
        return current;
    }

    public void setCurrent(SystemParameter current) {
        this.current = current;
    }

    public List<SystemParameter> getParamConfigs() {
        return paramConfigs;
    }

    public void setParamConfigs(List<SystemParameter> paramConfigs) {
        this.paramConfigs = paramConfigs;
    }

    public SystemParameter getSelected() {
        return selected;
    }

    public void setSelected(SystemParameter selected) {
        this.selected = selected;
    }

    public SystemParameter getCurrentRoot() {
        return currentRoot;
    }

    public void setCurrentRoot(SystemParameter currentRoot) {
        this.currentRoot = currentRoot;
    }

}
